package top.tagao.features_05_time;

import org.junit.jupiter.api.Test;

import java.awt.*;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author fu-xiao-liu
 * @Date 2022/3/21 10:14
 */
public class Time_04_Use {
    /**
     * @date   1. LocalDate、LocalTime、LocalDateTime
     */
    @Test
    public void test1() {
        LocalDateTime dateTime1 = LocalDateTime.now();
        System.out.println(dateTime1);

        LocalDateTime dateTime2 = LocalDateTime.of(2016, 11, 21, 10, 10);
        System.out.println(dateTime2);
        //增加
        LocalDateTime dateTime3 = dateTime2.plusDays(5);
        System.out.println(dateTime3);
        //减少

        LocalDateTime dateTime4 = dateTime3.minusDays(2);
        System.out.println(dateTime4);
    }

    //2. Instant : 时间戳。 （使用 Unix 元年  1970年1月1日 00:00:00 所经历的毫秒值）
    @Test
    public void test2() {
        Instant ins = Instant.now();  //默认使用 UTC 时区
        System.out.println(ins);
        System.out.println(ins.getEpochSecond());
        System.out.println(ins.toEpochMilli());

        Instant ins2 = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8));  //东8区
        System.out.println(ins2);
        System.out.println(ins2.getEpochSecond());
        System.out.println(ins2.toEpochMilli());
    }

    //3.Duration : 用于计算两个“时间”间隔  Period : 用于计算两个“日期”间隔
    @Test
    public void test3() throws InterruptedException {
        Instant ins1 = Instant.now();
        System.out.println("------ sleep(1000)--------------");
        Thread.sleep(1000);
        Instant ins2 = Instant.now();
        System.out.println("所耗费时间为：" + Duration.between(ins1, ins2));
        System.out.println("----------------------------------");

        LocalDate ld1 = LocalDate.now();
        LocalDate ld2 = LocalDate.of(2011, 1, 1);

        System.out.println(Period.between(ld1,ld2));

    }


    //4. TemporalAdjuster : 时间校正器
    @Test
    public void test4() {
        LocalDateTime ldt1 = LocalDateTime.now();
        System.out.println(ldt1);

        LocalDateTime ldt2 = ldt1.withDayOfMonth(10);
        System.out.println(ldt2);

        //TemporalAdjusters 静态方法对TemporalAdjuster的实现 | 下个周日
        LocalDateTime ldt3 = ldt1.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
        System.out.println(ldt3);

        //TemporalAdjuster 自定义：下个工作日
        LocalDateTime ldt4 = ldt1.with(l->{
            LocalDateTime ldt5 = (LocalDateTime) l;

            DayOfWeek dayOfWeek = ldt5.getDayOfWeek();
            if(dayOfWeek.equals(DayOfWeek.FRIDAY)){
                return ldt5.plusDays(3);
            }else if(dayOfWeek.equals(DayOfWeek.SATURDAY)){
                return ldt5.plusDays(2);
            }else{
                return ldt5.plusDays(1);
            }
        });
        System.out.println(ldt4);
    }

    //5. DateTimeFormatter : 解析和格式化日期或时间
    @Test
    public void test5(){
        LocalDateTime ldt = LocalDateTime.now();

        //API的日期格式
        DateTimeFormatter dtf1 = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        String strDate1 = ldt.format(dtf1);
        System.out.println(strDate1);

        //自定义日期格式
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss E");
        String strDate2 = ldt.format(dtf2);
        System.out.println(strDate2);

        //字符串时间转换为LocalDateTime
        LocalDateTime dateTime =  LocalDateTime.parse(strDate2, dtf2);
        System.out.println(dateTime);
    }

    //6.ZonedDate、ZonedTime、ZonedDateTime ： 带时区的时间或日期
    @Test
    public void test6(){
        //获取所有的时区
        Set<String> set = ZoneId.getAvailableZoneIds();
        set.forEach(System.out::println);

        LocalDateTime ldt = LocalDateTime.now(ZoneId.of("US/Pacific"));
        System.out.println(ldt);

        ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("US/Pacific"));
        System.out.println(zdt);

    }
}
